"""
GitCode 搜索API
"""
from typing import Any, Dict, List, Optional

from ..base.client import BaseAPI


class SearchAPI(BaseAPI):
    """搜索API"""
    
    async def search_repositories(
        self,
        query: str,
        sort: Optional[str] = None,
        order: Optional[str] = None,
        per_page: Optional[int] = None,
        page: Optional[int] = None
    ) -> Dict[str, Any]:
        """
        搜索仓库
        
        Args:
            query: 搜索关键词
            sort: 排序字段 ('stars'|'forks'|'updated')
            order: 排序方向 ('asc'|'desc')
            per_page: 每页结果数
            page: 页码
            
        Returns:
            搜索结果
        """
        params = {
            "q": query,
            "sort": sort,
            "order": order,
            "per_page": per_page,
            "page": page
        }
        params = {k: v for k, v in params.items() if v is not None}
        
        return await self.request("GET", "/search/repositories", params=params)
    
    async def search_code(
        self,
        query: str,
        sort: Optional[str] = None,
        order: Optional[str] = None,
        per_page: Optional[int] = None,
        page: Optional[int] = None
    ) -> Dict[str, Any]:
        """
        搜索代码
        
        Args:
            query: 搜索关键词
            sort: 排序字段 ('indexed')
            order: 排序方向 ('asc'|'desc')
            per_page: 每页结果数
            page: 页码
            
        Returns:
            搜索结果
        """
        params = {
            "q": query,
            "sort": sort,
            "order": order,
            "per_page": per_page,
            "page": page
        }
        params = {k: v for k, v in params.items() if v is not None}
        
        return await self.request("GET", "/search/code", params=params)
    
    async def search_issues(
        self,
        query: str,
        sort: Optional[str] = None,
        order: Optional[str] = None,
        per_page: Optional[int] = None,
        page: Optional[int] = None
    ) -> Dict[str, Any]:
        """
        搜索Issues和Pull Requests
        
        Args:
            query: 搜索关键词
            sort: 排序字段 ('created'|'updated'|'comments')
            order: 排序方向 ('asc'|'desc')
            per_page: 每页结果数
            page: 页码
            
        Returns:
            搜索结果
        """
        params = {
            "q": query,
            "sort": sort,
            "order": order,
            "per_page": per_page,
            "page": page
        }
        params = {k: v for k, v in params.items() if v is not None}
        
        return await self.request("GET", "/search/issues", params=params)
    
    async def search_users(
        self,
        query: str,
        sort: Optional[str] = None,
        order: Optional[str] = None,
        per_page: Optional[int] = None,
        page: Optional[int] = None
    ) -> Dict[str, Any]:
        """
        搜索用户
        
        Args:
            query: 搜索关键词
            sort: 排序字段 ('followers'|'repositories'|'joined')
            order: 排序方向 ('asc'|'desc')
            per_page: 每页结果数
            page: 页码
            
        Returns:
            搜索结果
        """
        params = {
            "q": query,
            "sort": sort,
            "order": order,
            "per_page": per_page,
            "page": page
        }
        params = {k: v for k, v in params.items() if v is not None}
        
        return await self.request("GET", "/search/users", params=params)
    
    async def search_commits(
        self,
        query: str,
        sort: Optional[str] = None,
        order: Optional[str] = None,
        per_page: Optional[int] = None,
        page: Optional[int] = None
    ) -> Dict[str, Any]:
        """
        搜索提交
        
        Args:
            query: 搜索关键词
            sort: 排序字段 ('author-date'|'committer-date')
            order: 排序方向 ('asc'|'desc')
            per_page: 每页结果数
            page: 页码
            
        Returns:
            搜索结果
        """
        params = {
            "q": query,
            "sort": sort,
            "order": order,
            "per_page": per_page,
            "page": page
        }
        params = {k: v for k, v in params.items() if v is not None}
        
        return await self.request("GET", "/search/commits", params=params)
    
    async def search_topics(
        self,
        query: str,
        per_page: Optional[int] = None,
        page: Optional[int] = None
    ) -> Dict[str, Any]:
        """
        搜索主题
        
        Args:
            query: 搜索关键词
            per_page: 每页结果数
            page: 页码
            
        Returns:
            搜索结果
        """
        params = {
            "q": query,
            "per_page": per_page,
            "page": page
        }
        params = {k: v for k, v in params.items() if v is not None}
        
        return await self.request("GET", "/search/topics", params=params) 